package com.hps.demo.netty.codec;

import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

/**
 * @author heps
 * @since 2020/8/15
 */
public class JSONDecoder extends LengthFieldBasedFrameDecoder {

  public JSONDecoder() {
    super(65535, 0, 4, 0, 4);
  }

  @Override
  protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf decode = (ByteBuf) super.decode(ctx, in);
    if (decode==null){
      return null;
    }
    int data_len = decode.readableBytes();
    byte[] bytes = new byte[data_len];
    decode.readBytes(bytes);
    return JSON.parse(bytes);
  }
}
